package net.zabuchy.authenticationHelper;

import java.util.Collection;
import java.util.Iterator;

import net.zabuchy.loginHelper.UserRoles;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;


public class AuthenticationHelper {
	
	public Collection<GrantedAuthority> getGrantedAuthority() {
		
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		Collection<GrantedAuthority> grantedAuthority = auth.getAuthorities();
		
		return grantedAuthority;
		
	}
	
	public String getName() {
		
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		return auth.getName();
		
	}
	
	public UserRoles getUserRole() {
		
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		Collection<GrantedAuthority> grantedAuthority = auth.getAuthorities();
		
		Iterator it = grantedAuthority.iterator();
		
		while(it.hasNext()) {
			if(it.next().equals("ROLE_ADMIN")) {
				return UserRoles.ROLE_ADMIN;
			} 
			else if(it.next().equals("PROJECT_MANAGER")) {
				return UserRoles.PROJECT_MANAGER;
			}
			else if(it.next().equals("QUALITY_ASSURANCE")) {
				return UserRoles.QUALITY_ASSURANCE;
			}
			else if(it.next().equals("DEVELOPER")) {
				return UserRoles.DEVELOPER;
			}
		}
		
		return null;
		
	}
	
	
	
}